<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="工单号" prop="workOrderNo">
        <el-input
          v-model="queryParams.workOrderNo"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品品号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品品号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="workOrderStatus">
        <el-select
          v-model="queryParams.workOrderStatus"
          placeholder="请选择工单状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OPTICORE_WORKORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产线" prop="line">
        <el-input
          v-model="queryParams.line"
          placeholder="请输入产线"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" width="60px" type="index" align="center">
        <template #default="scope">
          {{ (queryParams.pageNo - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="工单号" align="center" prop="workOrderNo" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="源订单" align="center" prop="sourceOrder" />
      <el-table-column label="销售订单" align="center" prop="salesOrder" />
      <el-table-column
        label="工单日期"
        align="center"
        prop="workOrderDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="产品品号" align="center" prop="productCode" />
      <el-table-column
        label="承诺交期"
        align="center"
        prop="promisedDelivery"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="BOM版次" width="90px" prop="bomVersion" />
      <el-table-column label="图号" align="center" prop="drawingNo" />
      <el-table-column label="工单数量" align="center" prop="workOrderQuantity" />
      <el-table-column label="工单状态" align="center" prop="workOrderStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OPTICORE_WORKORDER_STATUS" :value="scope.row.workOrderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="产线" align="center" prop="line">
        <template #default="scope">
          {{ formatRowValue(scope.row.line) }}
        </template>
      </el-table-column>
      <el-table-column
        label="下发时间"
        align="center"
        prop="issueTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="开工时间"
        align="center"
        prop="startProductionTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column label="开工人员" align="center" prop="startProductionWorker">
        <template #default="scope">
          {{ formatRowValue(scope.row.startProductionWorker) }}
        </template>
      </el-table-column>
      <el-table-column label="完成数量占比" align="center" width="110px" prop="completedPercentage"
                       :formatter="formatPercentage" />
      <el-table-column label="生产时长" align="center" prop="productionDuration" :formatter="formatDuration" />
      <el-table-column label="在制品数量" align="center" width="100px" prop="inProcessQuantity">
        <template #default="scope">
          {{ formatRowValue(scope.row.inProcessQuantity) }}
        </template>
      </el-table-column>
      <el-table-column label="已完成数量" align="center" width="100px" prop="completedQuantity">
        <template #default="scope">
          {{ formatRowValue(scope.row.completedQuantity) }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ExecuteReportForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ExecuteReportApi, ExecuteReportVO } from '@/api/opticore/executereport'
import ExecuteReportForm from './ExecuteReportForm.vue'

/** 生产执行报 列表 */
defineOptions({ name: 'ExecuteReport' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ExecuteReportVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  workOrderNo: undefined,
  productCode: undefined,
  workOrderStatus: undefined,
  line: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ExecuteReportApi.getExecuteReportPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()

const formatDuration = (row) => {
  if (row.productionDuration == null) return '-'
  const hours = (row.productionDuration / 3600).toFixed(2)
  console.log(hours)
  return `${hours} h`
}

const formatPercentage = (row) => {
  if (row.completedPercentage == null) return '-'
  return `${row.completedPercentage}%`
}

const formatRowValue = (object) => {
  if (object == null) return '-'
  return object
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
